clear all
cd "/Users/49066413/Dropbox/Dataverse_AEFIK"



**********************************************************************************************************************************************
**********************************************************************************************************************************************
*PLEASE READ: All datasets in this code are pseudo datasets.
**********************************************************************************************************************************************
**********************************************************************************************************************************************




**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for pseudo firm-level dataset
**********************************************************************************************************************************************
**********************************************************************************************************************************************

use firm_level_data, clear
tsset gvkey year

sort gvkey year
merge 1:1 gvkey year using sue_data
keep if _merge==3
drop _merge

*Sue_data is the dataset containing the EPS surprises

cap drop change_tfp1
cap drop change_tfp1W
gen change_tfp1=F1.tfp-L1.tfp
winsor change_tfp1, gen(change_tfp1W) p(0.01)

cap drop change_tfp2
cap drop change_tfp2W
gen change_tfp2=(F2.tfp+F1.tfp)/2-L1.tfp
winsor change_tfp2, gen(change_tfp2W) p(0.01)

cap drop change_tfp3
cap drop change_tfp3W
gen change_tfp3=(F3.tfp+F2.tfp+F1.tfp)/3-L1.tfp
winsor change_tfp3, gen(change_tfp3W) p(0.01)


cap drop change_inv1
cap drop change_inv1W
gen change_inv1=(F1.capex-L1.capex)/L1.capital_stock
winsor change_inv1, gen(change_inv1W) p(0.01)


cap drop change_inv2
cap drop change_inv2W
gen change_inv2=((F2.capex+F1.capex)/2-L1.capex)/L1.capital_stock
winsor change_inv2, gen(change_inv2W) p(0.01)

cap drop change_inv3
cap drop change_inv3W
gen change_inv3=((F3.capex+F2.capex+F1.capex)/3-L1.capex)/L1.capital_stock
winsor change_inv3, gen(change_inv3W) p(0.01)


cap drop change_emp1
cap drop change_emp1W
gen change_emp1=(F1.payroll-L1.payroll)/L1.capital_stock
winsor change_emp1, gen(change_emp1W) p(0.01)


cap drop change_emp2
cap drop change_emp2W
gen change_emp2=((F2.payroll+F1.payroll)/2-L1.payroll)/L1.capital_stock
winsor change_emp2, gen(change_emp2W) p(0.01)

cap drop change_emp3
cap drop change_emp3
gen change_emp3=((F3.payroll+F2.payroll+F1.payroll)/3-L1.payroll)/L1.capital_stock
winsor change_emp3, gen(change_emp3W) p(0.01)

cap drop sue_repo_adj_prcwXleft
gen sue_repo_adj_prcwXleft=negative_eps*sue_repo_adj_prcw

save firm_level_data_final, replace





**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for pseudo plant-level dataset
**********************************************************************************************************************************************
**********************************************************************************************************************************************

use plant_level_data, clear


sort gvkey year
merge m:1 gvkey year using sue_data
keep if _merge==3
drop _merge


sort plant year
tsset plant year


cap drop change_tfp3
cap drop change_tfp3W
gen change_tfp3=(F3.tfp+F2.tfp+F1.tfp)/3-L1.tfp
winsor change_tfp3, gen(change_tfp3W) p(0.01)




cap drop change_inv
cap drop change_invW
gen change_inv=((F3.capex+F2.capex+F1.capex)/3-L1.capex)/L1.capital_stock
winsor change_inv, gen(change_invW) p(0.01)

cap drop change_machine_inv
cap drop change_machine_invW
gen change_machine_inv=((F3.machine_capex+F2.machine_capex+F1.machine_capex)/3-L1.machine_capex)/L1.capital_stock
winsor change_machine_inv, gen(change_machine_invW) p(0.01)

cap drop change_comp_inv
cap drop change_comp_invW
gen change_comp_inv=((F3.comp_capex+F2.comp_capex+F1.comp_capex)/3-L1.comp_capex)/L1.capital_stock
winsor change_comp_inv, gen(change_comp_invW) p(0.01)




cap drop change_emp
cap drop change_empW
gen change_emp=((F3.payroll+F2.payroll+F1.payroll)/3-L1.payroll)/L1.capital_stock
winsor change_emp, gen(change_empW) p(0.01)


cap drop change_prod_emp
cap drop change_prod_empW
gen change_prod_emp=((F3.prod_payroll+F2.prod_payroll+F1.prod_payroll)/3-L1.prod_payroll)/L1.capital_stock
winsor change_prod_emp, gen(change_prod_empW) p(0.01)

cap drop change_nonprod_emp
cap drop change_nonprod_empW
gen change_nonprod_emp=((F3.nonprod_payroll+F2.nonprod_payroll+F1.nonprod_payroll)/3-L1.nonprod_payroll)/L1.capital_stock
winsor change_nonprod_emp, gen(change_nonprod_empW) p(0.01)

cap drop sue_repo_adj_prcwXleft
gen sue_repo_adj_prcwXleft=negative_eps*sue_repo_adj_prcw

save plant_level_data_final, replace




**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for Table 4 and  Table 5
**********************************************************************************************************************************************
**********************************************************************************************************************************************

use plant_level_data_final, clear

sort plant year
tsset plant year

cap drop median_tfp
egen median_tfp=median(tfp), by(gvkey year)
cap drop productive
gen productive=(tfp>median_tfp )
cap drop unproductive
gen unproductive=1-productive
cap drop median_tfp

cap drop productive_lag
gen productive_lag=L1.productive

cap drop unproductive_lag
gen unproductive_lag=L1.unproductive

cap drop inter1_table4
gen inter1_table4=negative_eps*productive_lag
cap drop inter2_table4
gen inter2_table4=negative_eps*unproductive_lag
cap drop inter3_table4
gen inter3_table4=sue_repo_adj_prcw*unproductive_lag
cap drop inter4_table4
gen inter4_table4=sue_repo_adj_prcw*negative_eps
cap drop inter5_table4
gen inter5_table4=sue_repo_adj_prcw*unproductive_lag*negative_eps


save plant_level_data_final, replace



**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for Table 7
**********************************************************************************************************************************************
**********************************************************************************************************************************************

use firm_level_data_final, clear
tsset gvkey year
cap drop median_rtw1
egen median_rtw1=median(pct_plants_RTW), by( year)
cap drop above_med_rtw1
gen above_med_rtw1=(pct_plants_RTW>median_rtw1 )
cap drop median_rtw1

cap drop median_rtw2
egen median_rtw2=median(pct_prodhours_RTW), by( year)
cap drop above_med_rtw2
gen above_med_rtw2=(pct_prodhours_RTW>median_rtw2 )
cap drop median_rtw2


cap drop diff
gen diff=F1.log_emp-L1.log_emp
cap drop emp_cut
gen emp_cut=(diff<0)

cap drop inter_table7
gen inter_table7=negative_eps*emp_cut

save firm_level_data_final, replace



**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for Table 8
**********************************************************************************************************************************************
**********************************************************************************************************************************************

use firm_level_data_final, clear
sort gvkey year
merge 1:1 gvkey year using financial_constraints_data
keep if _merge==3
drop _merge

save firm_level_data_final, replace



use plant_level_data_final, clear
sort gvkey year
merge m:1 gvkey year using financial_constraints_data
keep if _merge==3
drop _merge

save plant_level_data_final, replace



**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for Table 9
**********************************************************************************************************************************************
**********************************************************************************************************************************************


use firm_level_data_final, clear
sort gvkey year
merge 1:1 gvkey year using investor_horizon_data
keep if _merge==3
drop _merge

save firm_level_data_final, replace



use plant_level_data_final, clear
sort gvkey year
merge m:1 gvkey year using investor_horizon_data
keep if _merge==3
drop _merge

save plant_level_data_final, replace



**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for Table 10
**********************************************************************************************************************************************
**********************************************************************************************************************************************


use firm_level_data_final, clear
sort gvkey year
merge 1:1 gvkey year using ceo_eps_data
keep if _merge==3
drop _merge

save firm_level_data_final, replace



use plant_level_data_final, clear
sort gvkey year
merge m:1 gvkey year using ceo_eps_data
keep if _merge==3
drop _merge

save plant_level_data_final, replace



**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Cleaning for Table 11
**********************************************************************************************************************************************
**********************************************************************************************************************************************

use firm_level_data_final, clear

cap drop median1
egen median1=median(wf_tfp_sd), by( year)
cap drop high_wf_tfp_sd
gen high_wf_tfp_sd=(wf_tfp_sd>median1 )
cap drop median1



cap drop inter_table12
gen inter_table12=negative_eps*inv_cut_hhi

save firm_level_data_final, replace



**********************************************************************************************************************************************
**********************************************************************************************************************************************
*Final cleaning
**********************************************************************************************************************************************
**********************************************************************************************************************************************

use firm_level_data_final, clear
keep if year>=1988&year<=2010
save firm_level_data_final, replace


use plant_level_data_final, clear
keep if year>=1988&year<=2010
save plant_level_data_final, replace

